﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace Squog_1._1.Managers
{
    public struct SpriteSheet
    {
        public Texture2D Texture;
        public int Width;
        public int Height;
        public int Rows;
        public int Columns;
    }

    public class DrawManager : DrawableGameComponent
    {
        //Hashed list of all the available spritesheets
        private static Dictionary<String, SpriteSheet> SpriteSheets;
        public static ContentManager Content;
        public static SpriteBatch spriteBatch { get; private set; }
        public static SpriteFont Font;
        public static Rectangle BGRectangle;

        public static Texture2D Background;

        private static List<DrawableObject> Elements;
        private static Vector2 CameraPosition;

        //private static Terrain terrain;

        public static bool LockCamera { get; set; }

        protected override void  LoadContent()
        {
            SpriteSheets = new Dictionary<string, SpriteSheet>();
            Elements = new List<DrawableObject>();
            CameraPosition = Vector2.Zero;
            LockCamera = false;

            spriteBatch = new SpriteBatch(GraphicsDevice);

            LoadSpriteSheet(@"Sprites/dot", 1, 1);
            Font = Content.Load<SpriteFont>(@"Menu/TextFont");

            base.LoadContent();
        }

        public DrawManager(Game game)
            : base(game)
        {
            Content = game.Content;
        }

        public static void LoadSpriteSheet(String AssetName, int Columns, int Rows)
        {
            if (SpriteSheets.ContainsKey(AssetName))
                return;
            SpriteSheet newSheet = new SpriteSheet();
            newSheet.Texture = Content.Load<Texture2D>(AssetName);
            newSheet.Columns = Columns;
            newSheet.Rows = Rows;
            newSheet.Height = newSheet.Texture.Height / newSheet.Rows;
            newSheet.Width = newSheet.Texture.Width / newSheet.Columns;
            SpriteSheets.Add(AssetName, newSheet);
        }

        public static SpriteSheet GetSpriteSheet(String AssetName)
        {
            SpriteSheet ReturnSheet = new SpriteSheet();
            SpriteSheets.TryGetValue(AssetName, out ReturnSheet);
            return ReturnSheet;
        }

        public override void Update(GameTime gameTime)
        {
            //foreach (DrawableObject o in Elements)
            //    o.AnimationUpdate(gameTime);

            //Vector2 SquogScreenPos = Game1.Squog.Position - CameraPosition;
            Vector2 SafeZoneOffset = new Vector2(Settings.SafeZone.X, Settings.SafeZone.Y);

            //if (LockCamera)
            //{
            //    int o = 230;
            //    if (SquogScreenPos.X > Settings.SafeZone.Right - o - (Game1.Squog.SourceRectangle.Width * Game1.Squog.Scale))
            //        CameraPosition.X = Game1.Squog.Position.X + (Game1.Squog.SourceRectangle.Width * Game1.Squog.Scale) + o - Settings.SafeZone.Right;
            //    if (SquogScreenPos.X < Settings.SafeZone.Left + o)
            //        CameraPosition.X = Game1.Squog.Position.X - Settings.SafeZone.X - o;
            //    if (SquogScreenPos.Y > Settings.SafeZone.Bottom - o - (Game1.Squog.SourceRectangle.Height * Game1.Squog.Scale))
            //        CameraPosition.Y = Game1.Squog.Position.Y + (Game1.Squog.SourceRectangle.Height * Game1.Squog.Scale) + o - Settings.SafeZone.Bottom;
            //    if (SquogScreenPos.Y < Settings.SafeZone.Top + o)
            //        CameraPosition.Y = Game1.Squog.Position.Y - Settings.SafeZone.Y - o;
            //}

            base.Update(gameTime);
        }

        public static void Add(DrawableObject element)
        {
            DepthComparer comp = new DepthComparer();
            Elements.Add(element);
            Elements.Sort(comp);
        }

        public static void Remove(DrawableObject element)
        {
            Elements.Remove(element);
        }

        private class DepthComparer : Comparer<DrawableObject>
        {
            public override int Compare(DrawableObject x, DrawableObject y)
            {
                return y.Depth.CompareTo(x.Depth);
            }
        }

        public static void Clear()
        {
            Elements.Clear();
            //terrain = null;
        }

        public override void Draw(GameTime gameTime)
        {
            //BGRectangle = new Rectangle(0, 0, WindowManager.GraphicsDevice.PresentationParameters.BackBufferWidth, Game1.windowManager.GraphicsDevice.PresentationParameters.BackBufferHeight);
            //if (Background != null)
            //    DrawManager.spriteBatch.Draw(Background, BGRectangle, Color.White);

            //foreach (DrawableObject o in Elements)
            //{
            //    if (o.CameraIndependent)
            //        o.Draw(o.Position);
            //    else
            //        o.Draw((o.Position - CameraPosition) / o.Depth);
            //}

            //base.Draw(gameTime);

            //if (terrain != null)
            //    DrawTerrain(terrain);
        }

        //public static void DrawTerrain(Terrain t)
        //{
        //    List<LineSegment> L = t.GetLines();
        //    foreach (LineSegment l in L)
        //    {
        //        Vector2 dir = l.point1 - l.point2;
        //        float len = dir.Length();
        //        float theta = (float)Math.Asin(-dir.Y / len);
        //        Rectangle r = new Rectangle((int)(l.point1.X - CameraPosition.X), (int)(l.point1.Y - CameraPosition.Y), (int)len, 2);

        //        spriteBatch.Draw(GetSpriteSheet(@"Sprites/dot").Texture, r, new Rectangle(0, 0, 1, 1), Color.Red, theta, Vector2.Zero, SpriteEffects.None, 0.0f);
        //    }

        //}

        //public static void SetTerrain(Terrain terrain)
        //{

        //}
    }
}
